package com.vivo.ic.dm;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.vivo.ic.dm.tools.TestUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;

/**
 * Created by bruce on 2017/2/21.
 */
public class ChildDownloadManagerTest extends BaseTest{
    private static final String TAG = "Test";

    @Before
    public void setUp() throws Exception {
        // 删除数据库多余数据
        mResolver.delete(Downloads.Impl.CONTENT_URI, null, null);
    }

    @After
    public void tearDown() throws Exception {
        // 删除数据库多余数据
        mResolver.delete(Downloads.Impl.CONTENT_URI, null, null);
    }


    @Test
    public void testChildDownloadCallback() throws Exception {
        // 插测试值
        ContentValues values = TestUtils.getOkUrlDownloadValues();
        values.put(Downloads.Column.CONTROL, Downloads.Impl.CONTROL_RUN);
        Uri result = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
        assertNotNull(result);

        // 取出info
        final ContentResolver resolver = mContext.getContentResolver();
        final Cursor cursor = resolver.query(Downloads.Impl.CONTENT_URI,
                null, null, null, null);
        assertEquals(cursor.moveToFirst(), true);

        final DownloadInfo.Reader reader = new DownloadInfo.Reader(resolver, cursor);
        DownloadInfo info = reader.newDownloadInfo(mContext);
        cursor.close();

        ChildDownloadManager manager = new ChildDownloadManager(mContext, info);
        // 替换掉callback进行测试
        Log.i(TAG, "manager.mCoreSize " + manager.mCoreSize);
        CountDownLatch latch = new CountDownLatch(manager.mCoreSize);
        // 原有callback基础上加新代码
        manager.startDownload();

        latch.await(3, TimeUnit.SECONDS);

    }

    @Test
    public void startDownload() throws Exception {

    }

    @Test
    public void startChildDownload() throws Exception {

    }

    @Test
    public void shutdownAllChildDownloads() throws Exception {

    }

}